/*
 * @lc app=leetcode.cn id=1221 lang=javascript
 *
 * [1221] 分割平衡字符串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var balancedStringSplit = function (s) {
  let start = 0;
  let res = [];
  let stack = [];
  for (let i = 0; i < s.length; i++) {
    if (stack.length === 0
      || stack[stack.length - 1] === s.charAt(i)
    ) {
      stack.push(s.charAt(i));
    } else {
      stack.pop();
      if (stack.length === 0) {
        res.push(s.slice(start, i + 1));
        start = i + 1;
      }
    }
  }
  return res.length;
};
// @lc code=end

